# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

_hkgname=Agda
pkgname=agda
pkgver=2.6.4
pkgrel=2
pkgdesc="A dependently typed functional programming language and proof assistant"
url="https://wiki.portal.chalmers.se/agda/"
license=("custom")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'ghc' 'haskell-stmonadtrans' 'haskell-aeson' 'haskell-ansi-terminal'
         'haskell-async' 'haskell-blaze-html' 'haskell-boxes' 'haskell-case-insensitive'
         'haskell-cpphs' 'haskell-data-hash' 'haskell-dlist' 'haskell-edit-distance'
         'haskell-equivalence' 'haskell-gitrev' 'haskell-hashable' 'haskell-monad-control'
         'haskell-murmur-hash' 'haskell-parallel' 'haskell-peano' 'haskell-regex-tdfa'
         'haskell-split' 'haskell-strict' 'haskell-time-compat' 'haskell-unordered-containers'
         'haskell-uri-encode' 'haskell-vector' 'haskell-vector-hashtables' 'haskell-zlib'
         'haskell-text-icu')
optdepends=('agda-stdlib: for standard library')
makedepends=('uusi' 'alex' 'happy' 'ghc')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha512sums=('09b278dfb299779b02358e68a91f9d10e25083447810363ef0dbefdcdfc16070207c8c7272d2cb842ee77e67f1bd5d13007e3121c3f021107b4f6b6c4a82313a')

prepare() {
    cd $_hkgname-$pkgver
    sed -i '/fno-warn-overlapping-patterns/a \        , "-dynamic"' src/full/Agda/Compiler/MAlonzo/Compiler.hs
    uusi -u split $_hkgname.cabal
}

build() {
    cd $_hkgname-$pkgver

    # -fllvm is disabled because ghc+llc hangs since LLVM 13
    # https://gitlab.haskell.org/ghc/ghc/-/issues/20559
    runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
        --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname \
        --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fasm \
            -f-debug -fenable-cluster-counting -foptimise-heavily
    LC_CTYPE=en_US.UTF-8 runhaskell Setup build $MAKEFLAGS
    runhaskell Setup register --gen-script
    runhaskell Setup unregister --gen-script
    sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
    sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

package() {
    cd $_hkgname-$pkgver

    install -D -m744 register.sh   "${pkgdir}/usr/share/haskell/register/${pkgname}.sh"
    install -D -m744 unregister.sh "${pkgdir}/usr/share/haskell/unregister/${pkgname}.sh"
    LD_LIBRARY_PATH="$PWD/dist/build" runhaskell Setup copy --destdir="${pkgdir}"

    # Symlink to avoid writing to this place when agda-lib is present
    install -dm755 "$pkgdir"/usr/share/agda/lib/_build/$pkgver/agda
    ln -s ../../../prim "$pkgdir"/usr/share/agda/lib/_build/$pkgver/agda/

    install -D -m644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
    rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
